using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Module.CMS.Library;
using Framework.Module.CMS.Library.Definitions;
using Framework.Module.CMS.Library.Distributions;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Contents;
using SecurityURL;

namespace Framework.Module.CMS.Web.HomePage
{
    public partial class SearchAdvanceResult : CMSWebControl
    {
        public string lblPage = "";
        public int Total = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int totalRecord;
            if (Request.QueryString["page"] != null)
            {
                txtHiddenValue.Text = ConvertUtility.ToString((ConvertUtility.ToInt32(Request.QueryString["page"]) - 1));
            }
            else
            {
                txtHiddenValue.Text = "0";
            }
            string keysearch = "";
            try
            {
                SecureQueryString SQ = new SecureQueryString(Request["keysearch"]);
                keysearch = SQ["search"];
                lblSearch.Text = keysearch.ToUpper();
            }
            catch
            {
                keysearch = "";
            }
            
            if (keysearch == null || keysearch.Length == 0)
                return;

            int zoneopt = 0;
            //if (!Int32.TryParse(Request.QueryString["opt"], out zoneopt))
            //    return;

            int pagesize = 10;
            DataTable dtContents = DistributionController.SearchForWeb(pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord, keysearch);
            rptContentList.DataSource = dtContents;
            rptContentList.DataBind();

            this.pal1.Visible = rptContentList.Items.Count > 0;
            this.lab_mess.Visible = rptContentList.Items.Count <= 0;

            Total = totalRecord;
            #region paging

            double pagenum = totalRecord % pagesize;
            if (pagenum == 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 1; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            else if (totalRecord % pagesize > 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 2; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString(), false) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            #endregion
        }

        protected void rptContentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                Label lbalias = (Label)e.Item.FindControl("lbalias");
                lbalias.Text = (ConvertUtility.ToString(row["Content_Alias"]) == string.Empty ? "" : (row["Content_Alias"].ToString() + "  ")) + ConvertUtility.FormatTimeVn(ConvertUtility.ToDateTime(row["Distribution_CreateDate"]));

                int distid = ConvertUtility.ToInt32(row["Distribution_ID"]);
                int zoneid = ConvertUtility.ToInt32(row["Distribution_ZoneID"]);

                Image img = (Image)e.Item.FindControl("img_img");
                if (row["Content_Avatar"].ToString() != "")
                {
                    img.ImageUrl = MultimediaUtility.GetListAvatar(row["Content_Avatar"].ToString());
                    img.Attributes.Add("onclick", "javascript:window.location='" + BuildUrlByDistId(distid, zoneid) + "'");
                    img.Attributes.Add("style", "cursor:hand");
                }
                else img.Visible = false;

                HyperLink link1 = (HyperLink)e.Item.FindControl("link1");

                link1.Text = ConvertUtility.ToString(row["Content_Headline"]);
                link1.NavigateUrl = BuildUrlByDistId(distid, zoneid);

                HyperLink link2 = (HyperLink)e.Item.FindControl("link2");

                link2.NavigateUrl = BuildUrlByDistId(distid, zoneid);
            }
        }
    }
}